package org.android;

import static android.hardware.SensorManager.SENSOR_ACCELEROMETER;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

class ConnectedThread extends Thread implements SensorEventListener {
	private final BluetoothSocket mmSocket;
	private final OutputStream mmOutStream;
	private final BluetoothDevice mmDevice;
	private SensorManager manager;

	public ConnectedThread(BluetoothSocket socket, BluetoothDevice device) {
		mmSocket = socket;
		mmDevice = device;

		OutputStream tmpOut = null;
		try {
			tmpOut = socket.getOutputStream();
		} catch (IOException e) {
		}

		mmOutStream = tmpOut;

	}
  
	public void run() {
		byte[] buffer = new byte[2]; // buffer store for the stream
		Random r = new Random();
		for (int i = 0; i < 1000000; i++) {
			buffer[0] = (byte) r.nextInt(255);
			buffer[1] = (byte) r.nextInt(255);
			write(buffer);
		}
		cancel();

	}

	/* Call this from the main Activity to send data to the remote device */
	public void write(byte[] bytes) {
		try {
			mmOutStream.write(bytes);
		} catch (IOException e) {
		}
	}

	/* Call this from the main Activity to shutdown the connection */
	public void cancel() {
		try {
			mmSocket.close();
		} catch (IOException e) {
		}
	}

	@Override
	public void onAccuracyChanged(Sensor arg0, int arg1) {

	}

	@Override
	public void onSensorChanged(SensorEvent event) {

	}
}